// pages/bindMobile/bindMobile.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile_: '', //已经绑定手机号
    ipone: '',
    exchangeNum: '',
    limitGetSmsCode: {
      enable: true,
      leftSeconds: 60,
      content: "获取验证码"
    }
  },
  tab_phone() {
    this.setData({
      mobile_: '',
    })
  },
  getPhoneSmsByCode() {
    var _ = this;
    if (!_.data.limitGetSmsCode.enable) {
      wx.showToast({
        title: "请等待" + _.data.limitGetSmsCode.leftSeconds + "后再获取",
        icon: 'none',
        duration: 1000
      })
      return
    }
    var limitGetSmsCode2 = _.data.limitGetSmsCode
    limitGetSmsCode2.enable = false
    _.setData({
      limitGetSmsCode: limitGetSmsCode2
    })
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: app.globalData.user_id,
      phone: _.data.ipone,
      time: timestamp,
      type: '4',
      token: app.globalData.userToken,
    };
    let _token = axios.encryption(arys);
    let params = {
      type: '4',
      phone: _.data.ipone,
      userId: app.globalData.user_id,
      time: timestamp,
      token: app.globalData.userToken,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/sendRegCode",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        _.checkGetSmsFreq();
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 1000
        })
      } else {
        wx.showToast({
          title: res.data.text,
          icon: 'none',
          duration: 1000
        })
        var limitGetSmsCode2 = _.data.limitGetSmsCode
        limitGetSmsCode2.enable = true
        _.setData({
          limitGetSmsCode: limitGetSmsCode2
        })
      }
    })
  },
  checkGetSmsFreq() {
    var _ = this;
    var c = setInterval(function () {
      var limitGetSmsCode2 = _.data.limitGetSmsCode
      limitGetSmsCode2.content = --limitGetSmsCode2.leftSeconds + "秒后再获取";
      _.setData({
        limitGetSmsCode: limitGetSmsCode2
      })
      if (_.data.limitGetSmsCode.leftSeconds <= 0) {
        var limitGetSmsCode3 = _.data.limitGetSmsCode
        limitGetSmsCode3.content = "获取验证码";
        limitGetSmsCode3.leftSeconds = 60;
        _.setData({
          limitGetSmsCode: limitGetSmsCode3
        })
        clearInterval(c);
        _.data.limitGetSmsCode.enable = true;
        var limitGetSmsCode4 = _.data.limitGetSmsCode
        limitGetSmsCode4.enable = true;
        _.setData({
          limitGetSmsCode: limitGetSmsCode4
        })
      }
    }, 1000);
  },
  iponeInput(data) {
    this.setData({
      ipone: data.detail.value
    })
  },
  exchangeInput(data) {
    this.setData({
      exchangeNum: data.detail.value
    })
  },
  // 绑定手机号
  bindMobile() {
    let user_list = wx.getStorageSync('UserInformation')
    if (user_list.mobile) {
      // console.log('有手机号');
      this.mobile('2')
    } else {
      // console.log('没有手机号');
      this.mobile('1')
    }
  },
  // 绑定手机或换绑
  mobile(res) {
    if (!this.data.ipone) {
      wx.showToast({
        title: "手机号不能为空",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (!this.data.exchangeNum) {
      wx.showToast({
        title: "验证码不能为空",
        icon: 'none',
        duration: 1000
      })
      return
    }
    let _ = this;
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let arys = {
      userId: app.globalData.user_id,
      phone: _.data.ipone,
      time: timestamp,
      authCode: _.data.exchangeNum,
      type: res,
      token: app.globalData.userToken,
    };
    let _token = axios.encryption(arys);
    let params = {
      type: res,
      phone: _.data.ipone,
      userId: app.globalData.user_id,
      time: timestamp,
      authCode: _.data.exchangeNum,
      token: app.globalData.userToken,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/user/bindMobile",
      data: params
    }).then((res) => {
      if (res.data.status == 0) {
        this.setData({
          ipone: '',
          exchangeNum: ''
        })
      }
      wx.showToast({
        title: res.data.text,
        icon: 'none',
        duration: 1000
      })
      // console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let user_list = wx.getStorageSync('UserInformation')
    this.setData({
      mobile_: user_list.mobile
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
